package indi.zhifa.recipe.bailan.framework.auth.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.recipe.bailan.framework.auth.dao.mapper.RelUserRoleMapper;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IRelUserRoleDbService;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.RelUserRoleEntity;
import indi.zhifa.recipe.bailan.framework.mysql.entity.annotations.ZfDbService;
import indi.zhifa.recipe.bailan.framework.mysql.service.impl.ZfDbServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * <p>
 * 角色关联表 服务实现类
 * </p>
 *
 * @author 织法
 * @since 2022-12-19
 */
@RequiredArgsConstructor
@CacheConfig(cacheNames = "sys_rel_user_role")
@ZfDbService(name = "RelUserRoleEntity")
public class RelUserRoleDbServiceImpl extends ZfDbServiceImpl<RelUserRoleMapper, RelUserRoleEntity> implements IRelUserRoleDbService {

    @Cacheable(key = "#pUserId",condition = "#result!=null")
    @Override
    public List<RelUserRoleEntity> listByUserId(Long pUserId) {
        LambdaQueryWrapper<RelUserRoleEntity> queryWrapper = Wrappers.<RelUserRoleEntity>lambdaQuery()
                .eq(RelUserRoleEntity::getUserId,pUserId);
        List<RelUserRoleEntity> relUserRoleEntityList = list(queryWrapper);
        return relUserRoleEntityList;
    }

    @CachePut(key = "#pUserId")
    @Override
    public List<RelUserRoleEntity> edit(Long pUserId, List<RelUserRoleEntity> relRoles) {
        LambdaUpdateWrapper<RelUserRoleEntity> queryWrapper = Wrappers.<RelUserRoleEntity>lambdaUpdate()
                .eq(RelUserRoleEntity::getUserId,pUserId);
        remove(queryWrapper);
        saveBatch(relRoles);
        return relRoles;
    }

    @CacheEvict(key = "#pUserId")
    @Override
    public boolean deleteByUserId(Long pUserId) {
        LambdaUpdateWrapper<RelUserRoleEntity> queryWrapper = Wrappers.<RelUserRoleEntity>lambdaUpdate()
                .eq(RelUserRoleEntity::getUserId,pUserId);
        remove(queryWrapper);
        return true;
    }
}
